package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.baidu.mapapi.map.Text;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.task.GetUserDetailTask;
import com.xgkj.diyiketang.util.PalLog;
import com.xgkj.diyiketang.util.ShareUtil;
import com.xgkj.diyiketang.util.ToastUtil;

public class InvideVipActivity extends BaseActivity {

    private TextView mTvBack;
    private TextView mTvShouyi;
    private Button mShareBt;
    private LinearLayout pop_shade;
    private Handler mHandler = new Handler();
    private Context mContext;
    private PopupWindow popWindow;
    private String vipcode;
    private String vipFlower;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        if (intent != null) {
            vipcode = getIntent().getStringExtra("VIPCODE");
            Log.e("myfragment2:",vipcode+":");
        }
        getUserInfoIsLive();

        mContext = this;
        setContentView(R.layout.activity_invide_vip);
        initView();
    }

    private void initView() {

        pop_shade = (LinearLayout) findViewById(R.id.popwindow_shade);
        mTvBack = (TextView) findViewById(R.id.back);
        mTvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                InvideVipActivity.this.finish();
            }
        });
        mTvShouyi = (TextView) findViewById(R.id.shouyivip);
        mTvShouyi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
 Intent intent = new Intent(mContext,VipShouYiActivity.class);
                intent.putExtra("vipflower",vipFlower);
                ToastUtil.showMessage(mContext,vipFlower);
                startActivity(intent);
            }
        });
        mShareBt = (Button) findViewById(R.id.share_to_friend_btn);
        mShareBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String share_title = "邀请码："+vipcode;
                String myuserId = null;
               /* if (getEatShowApp().getmUserInfo() != null){
                    myuserId =  getEatShowApp().getmUserInfo().getUserId();
                }*/

                String share_url = "http://115.28.236.67:8099/classroom/shareVip.html?vipCode="+vipcode+"&device=android";
                // PalLog.e("share_url",share_url);
                ShareUtil.getInstance(mContext).show(share_title, share_url, new PopupWindow.OnDismissListener() {
                            @Override
                            public void onDismiss() {
                                pop_shade.setVisibility(View.GONE);
                            }
                        },
                        v);
            }
        });
        initPopWindow();
    }


    private void initPopWindow() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.popwindow,
                null);
        /*((Button) view.findViewById(R.id.takephoto)).setOnClickListener(this);
        ((Button) view.findViewById(R.id.choosephoto)).setOnClickListener(this);
        ((Button) view.findViewById(R.id.cancle)).setOnClickListener(this);*/
        popWindow = new PopupWindow(view, LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
//        popWindow.setBackgroundDrawable(new ColorDrawable());
        popWindow.setFocusable(true);
        popWindow.setOutsideTouchable(false);
//        popWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
//            @Override
//            public void onDismiss() {
//                if (mUploadMessage != null){
//                    PalLog.e(TAG,"777777777");
//                    mUploadMessage.onReceiveValue(null);
//                    mUploadMessage = null;
//                }
//            }
//        });
    }
private int isLive = -1;
    /*直播的 判断方法请求*/
    public void getUserInfoIsLive() {
        GetUserDetailTask task = new GetUserDetailTask();
        task.addListener(new ForgetUserInfoTaskEventListener(mContext));
        isLive = getEatShowApp().getTaskManager().addTask(task);

    }

    class ForgetUserInfoTaskEventListener extends ShowToastTaskEventListener {
        public ForgetUserInfoTaskEventListener(Context context) {
            super(context);
        }

        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if (taskId == isLive) {
                isLive = -1;

                Log.e("taskId:+result:+data::", "taskId" + taskId + "++" + result + ":::" + data.toString());
                // EatShowApp data1 = (EatShowApp) data;
                EatShowUserInfo data1 = (EatShowUserInfo) data;
                //canStartLive = data1.getCanStartLive();
                vipcode = data1.getVipCode() + "";
                vipFlower = data1.getVipFlower();

                if (getEatShowApp().getmUserInfo() != null) {
                    // getEatShowApp().getmUserInfo().setCanStartLive(canStartLive);

                }

            }
        }

    }
}
